package fit.deni.hibernate;

import java.util.Collection;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import fit.deni.beans.Clan;
import fit.deni.hibernate.interfaces.ClanService;

public class ClanServiceImpl implements ClanService {

	private Session session;

	public ClanServiceImpl(Session session) {
		this.session = session;
	}

	@CommitAfter
	public void addClient(Clan clan) {
		session.merge(clan);
	}

	public Collection<Clan> getAll() {
		return session.createCriteria(Clan.class).add(Restrictions.eq("deleted", false)).list();
	}

	@CommitAfter
	public void deleteClient(Clan clan) {
		clan.setDeleted(true);
		session.merge(clan);
//		session.delete(clan);
//		session.flush();
	}

	public Clan getClientById(int id) {
		return (Clan) session.createCriteria(Clan.class).add(Restrictions.eq("id", id)).list().get(0);
	}

}
